﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Entity;
using System.Linq;
using StoreManager.Models;
using System.Web.Mvc;
using PagedList;

namespace StoreManager.DAO
{   
    public class ProductDAO
    {
        private static StoreManagerContext db = new StoreManagerContext();
        /// <summary>
        /// Search product by Code and Name
        /// </summary>
        /// <param Name="searchCrireria">ProductSearchView</param>
        /// <returns>IPagedList</returns>
        public static IPagedList<Product> SearchProducts(ProductSearchView searchCrireria)
        {
            IPagedList<Product> pageList = null;
            var pageIndex = searchCrireria.Page ?? 1;
            if (string.IsNullOrEmpty(searchCrireria.Code))
            {
                if (string.IsNullOrEmpty(searchCrireria.Name))
                {
                    var products = db.Products.OrderBy(p => p.Code);
                    pageList = products.ToPagedList(pageIndex, SMConst.PAGE_SIZE);
                }
                else
                {
                    var products = db.Products.Where(p => p.Name.Contains(searchCrireria.Name)).OrderBy(p => p.Code);
                    pageList = products.ToPagedList(pageIndex, SMConst.PAGE_SIZE);
                }
            }
            else
            {
                if (string.IsNullOrEmpty(searchCrireria.Name))
                {
                    var products = db.Products.Where(p => p.Code.Contains(searchCrireria.Code)).OrderBy(p => p.Code);
                    pageList = products.ToPagedList(pageIndex, SMConst.PAGE_SIZE);
                }
                else
                {
                    //Add new Name param in search
                    var products = db.Products.Where(p => p.Code.Contains(searchCrireria.Code)).OrderBy(p => p.Code);
                    pageList = products.ToPagedList(pageIndex, SMConst.PAGE_SIZE);
                }
            }

            return pageList;
        }
    }
    public class OrderDAO
    {
        private static StoreManagerContext db = new StoreManagerContext();
        /// <summary>
        /// Search product by Code and Name
        /// </summary>
        /// <param Name="searchCrireria">ProductSearchView</param>
        /// <returns>IPagedList</returns>
        public static IPagedList<ImportOrder> SearchImportOrders(ImportOrderSearchView searchCrireria)
        {
            IPagedList<ImportOrder> pageList = null;
            var pageIndex = searchCrireria.Page ?? 1;
            if (string.IsNullOrEmpty(searchCrireria.ImportOrderNo))
            {
                if (string.IsNullOrEmpty(searchCrireria.Status))
                {
                    var orders = db.ImportOrders.OrderBy(o => o.ImportOrderNo);
                    pageList = orders.ToPagedList(pageIndex, SMConst.PAGE_SIZE);
                }
                else
                {
                    var orders = db.ImportOrders.Where(p => p.ImportOrderNo.Contains(searchCrireria.Status)).OrderBy(p => p.ImportOrderNo);
                    pageList = orders.ToPagedList(pageIndex, SMConst.PAGE_SIZE);
                }
            }
            else
            {
                if (string.IsNullOrEmpty(searchCrireria.Status))
                {
                    var orders = db.ImportOrders.Where(p => p.ImportOrderNo.Contains(searchCrireria.ImportOrderNo)).OrderBy(p => p.ImportOrderNo);
                    pageList = orders.ToPagedList(pageIndex, SMConst.PAGE_SIZE);
                }
                else
                {
                    //Add new Name param in search
                    var orders = db.ImportOrders.Where(p => p.ImportOrderNo.Contains(searchCrireria.ImportOrderNo)).OrderBy(p => p.ImportOrderNo);
                    pageList = orders.ToPagedList(pageIndex, SMConst.PAGE_SIZE);
                }
            }

            return pageList;
        }
    }
}